package com.bsi.rd.framework.zuul.redis;

import com.bsi.rd.framework.zuul.remote.PermissionServiceRemote;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by WangWei on 2018/12/12.
 */
@Service
public class RedisService {
    @Autowired
    private PermissionServiceRemote permissionServiceRemote;

    @Autowired
    protected RedisTemplate redisTemplate;

    public void initPermission(String userId) {
        List<Map<String, Object>> permissionList = permissionServiceRemote.getAllPermission(userId);
        if (CollectionUtils.isNotEmpty(permissionList)) {
            Map<String, List<Map<String, Object>>> permissionMap = new HashMap<String, List<Map<String, Object>>>();
            permissionMap.put(userId, permissionList);
            redisTemplate.opsForHash().putAll(userId, permissionMap);
            redisTemplate.expire(userId, 1, TimeUnit.HOURS);
        }
    }
}
